<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

require dirname(__FILE__).'/pc_controller.php';

class Hd_invitation_config extends PC_controller {

	protected $_config_view = 'config';

	public function __construct(){
		parent::__construct();
		$this->load->model('Configmodel','m_config');
	}

	public function index(){
		$data = parent::index();

		$assist = array();
		$assist['where_in']['config_name'] = array(HD_INVITATION_TYPE_1,HD_INVITATION_TYPE_2,HD_INVITATION_TYPE_3);
        $config_list = $this->m_config->getConfigList($assist);
        $config_arr = array();
        foreach ($config_list as $k => $v) {
            $v_arr = explode('|',$v['config_value']);
            $config_arr[] = array($v['config_name'].'_start',empty($v_arr[0]) ? '' : $v_arr[0]);
            $config_arr[] = array($v['config_name'].'_end',empty($v_arr[1]) ? '' : $v_arr[1]);
        }
		$data['attr']['config_list'] = json_encode($config_arr);

		$this->load->view('admin/hd_invitation_config',$data);
	}

	public function saveConfig(){
        $this->validate->setRule(HD_INVITATION_TYPE_1.'_start','string');
        $this->validate->setRule(HD_INVITATION_TYPE_1.'_end','string');
        $this->validate->setRule(HD_INVITATION_TYPE_2.'_start','string');
        $this->validate->setRule(HD_INVITATION_TYPE_2.'_end','string');
        $this->validate->setRule(HD_INVITATION_TYPE_3.'_start','string');
        $this->validate->setRule(HD_INVITATION_TYPE_3.'_end','string');
        if(!$this->validate->run($this->input->post())){
            $this->response->setDetail($this->validate->getMessage());
            $this->response->responseExit();
        }
        $info = $this->validate->getResult();

		$insert_arr = array();
		foreach (array(HD_INVITATION_TYPE_1,HD_INVITATION_TYPE_2,HD_INVITATION_TYPE_3) as $value) {
			$row = array(
				'user_id' => $this->m_app->getCurrentUserId(),
				'config_name' => $value,
				'config_value' => implode(array($info[$value.'_start'],$info[$value.'_end']),'|'),
			);
			$row['config_id'] = $this->m_config->editConfig($row);

			// $insert_arr[] = $row;
		}
		// $this->m_config->createConfigHistoryBatch($insert_arr);

		$this->response->setSuccess();
		$this->response->setDetail($this->lang->line('success_update'));
		$this->response->responseExit();
	}
}
